<?php

namespace app\admin\controller;

use app\admin\model\History_model;
use app\admin\model\Monitor;
use think\Controller;
use think\Db;

class History extends Controller
{
    public function searchHistory()
    {
        $historyModel = new History_model();
        $dataSet = $historyModel->group('update_time')->select();
        $titleItem = $historyModel->group('monitor_id')->select();
        $title = [];
        foreach ($titleItem as $item){
            $loop_name = Db::name('monitor')
                ->field(['loop_name'])
                ->where(['monitor_id'=>$item->monitor_id])
                ->find();
            $temp=[$loop_name['loop_name']=>
                [
                    ['title'=>'电流mA','data'=>$item->monitor_id.'_A'],
                    ['title'=>'温度℃','data'=>$item->monitor_id.'_t']
                ]
            ];
            $title = array_merge($title,$temp);
        }
        $record = [];
        foreach ($dataSet as $item) {
            $res = Db::name('history_model')
                ->field(['monitor_id','resi_current','cable_temp','update_time'])
                ->where(['update_time'=>$item->update_time])
                ->select();
            $recordItem = ['time'=>$res[0]['update_time']];
            foreach ($res as $data){
                $temp = [
                    $data['monitor_id'].'_A'=>$data['resi_current'],
                    $data['monitor_id'].'_t'=>$data['cable_temp']
                ];
                $recordItem = array_merge($recordItem,$temp);
            }
            array_push($record,$recordItem);
        }
        return [
            array_reverse($title),
            $record
        ];
    }

    public function query()
    {
        return $this->fetch('history/query');
    }
}